package zy.ejb.client;

import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import zy.ejb.HelloLocal;
import zy.ejb.HelloRemote;

public class InvokeTest {

	public static void main(String[] args) throws NamingException {

		HelloRemote hello = (HelloRemote) getService("Hello", HelloRemote.class);
		System.out.println(hello.sayHello_remote("我的cs client invoke ejb111"));
//		HelloLocal helloLocal = (HelloLocal) getService("Hello", HelloLocal.class);
//		System.out.println(helloLocal.sayHello_local("我的cs client invoke ejb 222"));
	}

	public static Object getService(String beanName, Class serviceClass) {
		Object retService = null;

		try {

			Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
			jndiProperties.put(Context.URL_PKG_PREFIXES,
					"org.jboss.ejb.client.naming");
			InitialContext context = new InitialContext(jndiProperties);
			final String appName = "ear1";
			final String moduleName = "ejb1";
			final String distinctName = "";
			// Object obj = context.lookup("ejb:" + appName + "/" + moduleName +
			// "/"
			// + distinctName + "/Hello!zy.ejb.HelloRemote");

			Object obj = context.lookup("ejb:" + appName + "/" + moduleName
					+ "/" + distinctName + "/" + beanName + "!"
					+ serviceClass.getName());

			return obj;
		} catch (NamingException ex) {
			ex.printStackTrace();
			return null;
		}
	}
}
